/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#pragma once

#ifndef SSI_SOCKETS_API
	#define SSI_SOCKETS_API __declspec(dllexport)
#endif

#include "SSI_Stream.h"
#include "SSI_Socket.h"
#include "SSI_ScriptAPI.h"

#include <stdio.h>

#include <iostream>
#include "sockcmd.h"
using std::string;

#define  SSI_OPEN_REQUEST      131
#define  SSI_LINE_REQUEST      132
#define  SSI_RESPONSE_OUTPUT   133
#define  SSI_COMMAND           134
#define  SSI_BYE               135
#define  SSI_ACTION_REQUEST    136
#define  SSI_ACTION_RESPONSE   137

#define  SSI_ERROR_OUTPUT      140
#define  SSI_WARNING_OUTPUT    141
#define  SSI_FAILURE_OUTPUT    142

#define  SSI_FILE_REQUEST			 150			 
#define  SSI_FILE_RESPONSE     151
#define  SSI_FILE_ERROR        152

#define  SSI_RSP_PORT          200
#define  SSI_FLAG_REQUEST      210
#define  SSI_FLAG_RESPONSE     211

#define  SSI_ARGS_REQUEST      220
#define  SSI_ARGS_RESPONSE     221

#define  SSI_VAR_RQST          300
#define  SSI_VAR_RSP           301

#define  SSI_DBG_RQST          500
#define  SSI_DBG_RSP           501
#define  SSI_DBG_STEP          510

#define  SSI_BPT_ADD           520
#define  SSI_BPT_RMV           521

#define  SSI_DBG_JMP           530
#define  SSI_DBG_OUT           531

#define  SSI_DBG_EVAL          540

#define  SSI_DBG_GOCTR         550
#define  SSI_DBG_TOLIN         551

#define  DEBUG_BIT    1
#define  ARGS_BIT     2


class SSI_SOCKETS_API socket_stream :
	public ssi_stream
{
public:
	virtual void Disp(const char* In, SSI_BOOL bAddLine = 1);
	virtual void operator <<(const char* In);
  virtual void operator >>(const char* In);
	virtual SSI_INT64 getline(SSI_CHAR* Buff, SSI_INT64 sz);
	virtual SSI_INT64 getDebug(SSI_INT64 LineNum, const SSI_CHAR* File, SSI_CHAR* Buff, SSI_INT64 sz);

	socket_stream(void);
	virtual ~socket_stream(void);
	virtual void ErrorDisp(const char* In, SSI_BOOL bAddLine);
	virtual void WarningDisp(const char* In, SSI_BOOL bAddLine);
	virtual void FailureDisp(const char* In, SSI_BOOL bAddLine);
	virtual int getLocalFile(const String& FileName, String& LocName);

	CSocketSSI* m_Socket;

	void SendOutput(const char *In, SSI_BOOL bAddLine, SSI_INT64 Type);

	//CSocketSSI m_Socket;
	//SSI_SOCKET m_Listener;
	//SSI_SOCKET m_Poster;

	//SSI_INT64 Init(const char* Host, SSI_INT64 Port);
	//SSI_SOCKET GetConnection(void);
	//SSI_SOCKET m_Connection;
	//void CleanUp(void);
	//void CloseConnection(void);
	int SetLogFile(const char* InFile);
	string sLogFile;
	void Log(const char* Input);
	void SetSocket(CSocketSSI* NewSocket);
	CSockCmd m_Cmd;
	SSI_INT64 getCmd();
	//SSI_INT64 getDebug();
	SSI_INT64 sendDebug();
private:
	CEngine* m_Engine;
public:
	void SetEngine(CEngine* InEngine);
	void CleanUp();
	void CloseConnection();
private:
	SSI_INT64 m_SentCtr;
	SSI_INT64 m_RecvCtr;
public:
	SSI_INT64 getSentCtr(void);
	SSI_INT64 getRecvCtr(void);
	SSI_INT64 Initialize(SSI_INT64& Type, SSI_INT64& Mode, String& Script, String& ScriptArgs);
};
